import numpy as np


class MinHash:

    def __init__(self, vertex, para):
        self.vertex = vertex
        self.perm_num = para["perm_num"]
        self.sampling = np.zeros(self.perm_num)
        self.mersenne_prime = np.uint64((1 << 31) - 1)

        gen = np.random.RandomState(seed=1)
        self.permutations = np.array([(gen.randint(1, self.mersenne_prime, dtype=np.uint64),
                                       gen.randint(0, self.mersenne_prime, dtype=np.uint64))
                                      for _ in range(self.perm_num)], dtype=np.uint64).T

    def add_vertex(self, vertex):
        changed = False
        for index in range(self.perm_num):
            value = (self.permutations[0][index] * vertex.vertex + self.permutations[1][index]) % self.mersenne_prime
            if value > self.sampling[index]:
                self.sampling[index] = vertex.vertex
                changed = True

        return changed

